home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1996 #6 / Amiga Plus CD - 1996 - No. 06.iso / pd / netz / dl / misc.c < prev    next >
C/C++ Source or Header  |  1992-09-02  |  5KB  |  252 lines

  1. /* Copyright (C) 1995, 1996 Zlatko Calusic <maverick@fly.cc.fer.hr>
  2. This file is part of DownLoad, FTP Client
  3.  
  4. The DownLoad is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the Free
  6. Software Foundation.
  7.  
  8. The DownLoad is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  11. more details.
  12.  
  13. You should have received a copy of the GNU General Public License along with
  14. the DownLoad source; see the file COPYING.  If not, write to the Free
  15. Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  16.  
  17. #include <math.h>
  18. #include "dl.h"
  19.  
  20. extern int local;
  21.  
  22. extern int parsed;
  23.  
  24. extern char url[], host[], path[], file[];
  25.  
  26. char *rate(int bytes, int seconds)
  27. {
  28.     int flag = 0;
  29.     char m_q[16], m_e[16], m_t[16];
  30.     double quantity, elapsed, transfer;
  31.     static char line[256];
  32.  
  33.     *line = 0;
  34.     if (bytes <= 0 || seconds < 0)
  35.         return line;
  36.     if (!seconds)
  37.         seconds++;
  38.     if (bytes < 1000)
  39.     {
  40.         quantity = bytes;
  41.         strcpy(m_q, "bytes");
  42.         flag |= 1;
  43.     }
  44.     else if (bytes >= 1000 && bytes < 1000000)
  45.     {
  46.         quantity = (double) bytes / 1000;
  47.         strcpy(m_q, "Kbytes");
  48.     }
  49.     else
  50.     {
  51.         quantity = (double) bytes / 1000000;
  52.         strcpy(m_q, "Mbytes");
  53.     }
  54.     if (seconds < 60)
  55.     {
  56.         elapsed = seconds;
  57.         strcpy(m_e, "seconds");
  58.         flag |= 2;
  59.     }
  60.     else if (seconds >= 60 && seconds < 3600)
  61.     {
  62.         elapsed = (double) seconds / 60;
  63.         strcpy(m_e, "minutes");
  64.     }
  65.     else
  66.     {
  67.         elapsed = (double) seconds / 3600;
  68.         strcpy(m_e, "hours");
  69.     }
  70.     transfer = (double) bytes / seconds;
  71.     if (transfer < 1000)
  72.         strcpy(m_t, "bytes/sec");
  73.     else if (transfer > 1000 && transfer < 1000000)
  74.     {
  75.         transfer /= 1000;
  76.         strcpy(m_t, "Kbytes/sec");
  77.     }
  78.     else
  79.     {
  80.         transfer /= 1000000;
  81.         strcpy(m_t, "Mbytes/sec");
  82.     }
  83.     switch (flag)
  84.     {
  85.         case 0:
  86.             sprintf(line, "Received %.2f %s in %.2f %s (%.2f %s).\n", quantity, m_q, elapsed, m_e, transfer, m_t);
  87.             break;
  88.         case 1:
  89.             sprintf(line, "Received %d %s in %.2f %s (%.2f %s).\n", (int) quantity, m_q, elapsed, m_e, transfer, m_t);
  90.             break;
  91.         case 2:
  92.             sprintf(line, "Received %.2f %s in %d %s (%.2f %s).\n", quantity, m_q, (int) elapsed, m_e, transfer, m_t);
  93.             break;
  94.         case 3:
  95.             sprintf(line, "Received %d %s in %d %s (%.2f %s).\n", (int) quantity, m_q, (int) elapsed, m_e, transfer, m_t);
  96.     }
  97.     return line;
  98. }
  99.  
  100. int parse(void)
  101. {
  102.     char *cptr;
  103.     int last = 0;
  104.  
  105.     if ((cptr = strchr(url, ' ')))
  106.         *cptr = ':';
  107.     while (*(strchr(url, 0) - last - 1) == ':')
  108.         last++;
  109.     if (strstr(url, "ftp://") == url)            /* line is in URL format */
  110.     {
  111.         strcpy(url, url + 6);
  112.         if ((cptr = strchr(url, '/')))
  113.             *cptr = ':';
  114.     }
  115.     if ((cptr = strtok(url, ":")))
  116.         strcpy(host, cptr);
  117.     else
  118.         return ERROR;                            /* ":" not found */
  119.     if ((cptr = strtok(NULL, ":")))
  120.         strcpy(path, cptr);
  121.     else
  122.         return ERROR;
  123.     while((cptr = strtok(NULL, ":")))
  124.     {
  125.         strcat(path, ":");
  126.         strcat(path, cptr);
  127.     }
  128.     while (last)
  129.     {
  130.         strcat(path, ":");
  131.         last--;
  132.     }
  133.     if (*(cptr = strchr(path, 0) - 1) == '/')
  134.         return ERROR;                            /* last character is "/" */
  135.     while (*--cptr != '/')
  136.         if (cptr < path)
  137.         {                                        /* "/" not found */
  138.             cptr = path - 1;
  139.             break;
  140.         }
  141.     strcpy(file, cptr + 1);
  142.     if (cptr > path)
  143.         *cptr = 0;
  144.     else
  145.         *path = 0;
  146.     parsed = 1;
  147.     return OK;
  148. }
  149.  
  150. /* Revised fnmatch.c, originally written by Brian Fox & Chet Ramey */
  151.  
  152. int match(char *pattern, char *string, int flag)
  153. {
  154.     char ch, *p = pattern, *n = string;
  155.  
  156.     while ((ch = *p++))
  157.     {
  158.         switch (ch)
  159.         {
  160.             case '?':
  161.                 if (!*n || (flag && *n == '.' && n == string))
  162.                     return ERROR;
  163.                 break;
  164.             case '\\':
  165.                 if (*n != (ch = *p++))
  166.                     return ERROR;
  167.                 break;
  168.             case '*':
  169.                 if (flag && *n == '.' && n == string)
  170.                     return ERROR;
  171.                 for (ch = *p++; ch == '?' || ch == '*'; ch = *p++, ++n)
  172.                     if (ch == '?' && !*n)
  173.                         return ERROR;
  174.                 if (!ch)
  175.                     return OK;
  176.                 {
  177.                     char ch1 = (ch == '\\') ? *p : ch;
  178.  
  179.                     for (--p; *n; ++n)
  180.                         if ((ch == '[' || *n == ch1) && match (p, n, (flag ? 0 : 1)) == OK)
  181.                             return OK;
  182.                     return ERROR;
  183.                 }
  184.             case '[':
  185.             {
  186.                 int not;
  187.  
  188.                 if (!*n || (flag && *n == '.' && n == string))
  189.                     return ERROR;
  190.                 {
  191.                     char *np;
  192.  
  193.                     for (np = p; np && *np && *np != ']'; np++);
  194.                         if (np && !*np)
  195.                         {
  196.                             if (*n != '[')
  197.                                 return ERROR;
  198.                             goto next_char;
  199.                         }
  200.                 }
  201.                 if ((not = (*p == '!' || *p == '^')))
  202.                     ++p;
  203.                 ch = *p++;
  204.                 for (;;)
  205.                 {
  206.                     char start = ch, end = ch;
  207.  
  208.                     if (ch == '\\')
  209.                         start = end = *p++;
  210.                     if (!ch)
  211.                         return ERROR;
  212.                     ch = *p++;
  213.                     if (ch == '-' && *p != ']')
  214.                     {
  215.                         if ((end = *p++) == '\\')
  216.                             end = *p++;
  217.                         if (!end)
  218.                             return ERROR;
  219.                         ch = *p++;
  220.                     }
  221.                     if (*n >= start && *n <= end)
  222.                         goto matched;
  223.                     if (ch == ']')
  224.                         break;
  225.                 }
  226.                 if (!not)
  227.                     return ERROR;
  228. matched:
  229.                 while (ch != ']')
  230.                 {
  231.                     if (!ch)
  232.                         return ERROR;
  233.                     if ((ch = *p++) == '\\')
  234.                         ++p;
  235.                 }
  236.                 if (not)
  237.                     return ERROR;
  238.             }
  239. next_char:
  240.                 break;
  241.             default:
  242.                 if (ch != *n)
  243.                     return ERROR;
  244.         }
  245.         ++n;
  246.     }
  247.     if (!*n)
  248.         return OK;
  249.     else
  250.         return ERROR;
  251. }
  252.